using AppToolKit.Model.Validation.Attribute;
using System;
using System.Diagnostics;
using System.Reflection;
namespace AppToolKit.Model
{
	public class EntityMetaHelper
	{
		public static EntityMeta GetMeta(Type type)
		{
			Debug.Assert(type != null);
			EntityMeta entityMeta = new EntityMeta();
			EntityMetaHelper.GetFieldMeta(type, entityMeta);
			return entityMeta;
		}
		private static void GetFieldLength(Type type, PropertyInfo prop, FieldMeta meta)
		{
			if (prop.IsDefined(typeof(StrLengthRuleAttribute), false))
			{
				object[] customAttributes = prop.GetCustomAttributes(typeof(StrLengthRuleAttribute), false);
				meta.Length = ((StrLengthRuleAttribute)customAttributes[0]).Length;
			}
		}
		private static void CollectValidator(Type type, EntityMeta meta)
		{
		}
		private static void GetFieldMeta(Type type, EntityMeta entity)
		{
			PropertyInfo[] properties = type.GetProperties();
			for (int i = 0; i < properties.Length; i++)
			{
				PropertyInfo propertyInfo = properties[i];
				if (propertyInfo.IsDefined(typeof(FieldDescriptAttribute), false))
				{
					FieldMeta fieldMeta = new FieldMeta();
					EntityMetaHelper.GetFieldDescript(type, propertyInfo, fieldMeta);
					EntityMetaHelper.GetFieldLength(type, propertyInfo, fieldMeta);
					entity[fieldMeta.Field] = fieldMeta;
				}
			}
		}
		private static void GetFieldDescript(Type type, PropertyInfo prop, FieldMeta meta)
		{
			object[] customAttributes = prop.GetCustomAttributes(typeof(FieldDescriptAttribute), false);
			FieldDescriptAttribute fieldDescriptAttribute = (FieldDescriptAttribute)customAttributes[0];
			meta.SetResource(fieldDescriptAttribute.ResoucesManager, type.Assembly);
			meta.Descript = fieldDescriptAttribute.Resouces;
			meta.Field = prop.Name;
			meta.FieldType = prop.PropertyType;
		}
	}
}
